package com.arm.controller.api.admin.role;

import com.arm.controller.BaseController;
import com.arm.module.Role;
import com.arm.service.AdminUserService;
import com.arm.service.MenuService;
import com.arm.service.RoleService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ResultObj;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by Andrew  on 2017/2/26.
 */
@Api(description = "后台用户角色接口")
@RestController
@RequestMapping(value = "/api/admin/role")
public class RoleController extends BaseController {
@Autowired
AdminUserService adminUserService;
  @Autowired
  MenuService menuService;
    @Autowired
    RoleService roleService;

    @ApiOperation(value = "获取角色列表", notes = "获取角色列表")
    @GetMapping(value="/")
    public ResultObj<List<Role>> Role() {
        ResultObj<List<Role>> resultObj = new ResultObj();
        List<Role> roleList = roleService.getRoleList(null);
        resultObj.setData(roleList);
        return  resultObj;
    }

    @ApiOperation(value = "新增角色", notes = "新增角色")
    @ApiImplicitParams({

            @ApiImplicitParam(name = "role", value = "新增角色", required = false, paramType = "body", dataType = "Role")
    })
    @PostMapping(value="/")
    public ResultObj addRole(@RequestBody Role role) {

        ResultObj resultObj = new ResultObj();
        try {



            roleService.modifyRole(role, role.getMenuIdList());

            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj;
    }
    @ApiOperation(value = "更新角色", notes = "更新角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "更新角色的Id", defaultValue = "1",required = false, paramType = "path", dataType = "int"),

            @ApiImplicitParam(name = "role", value = "更新的角色对象", required = false, paramType = "body", dataType = "Role")
    })
    @PutMapping(value="/{id}")
    public ResultObj modifyRole(@PathVariable("id") Long id, @RequestBody Role role) {
        ResultObj resultObj = new ResultObj();
        try {
            role.setTid(id);

            roleService.modifyRole(role, role.getMenuIdList());

            resultObj.setMsg("成功！");
        } catch (MyRunTimeExcption myRunTimeExcption) {
            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj;
    }
  @DeleteMapping(value="/deleteMenu")
    public String deleteMenu(HttpServletRequest request) {
        System.out.println("=================================getMenuListForRole==========================================>>>>>>");

        ResultObj resultObj = new ResultObj();
        try {
            Map<String, String> map = this.showRequestParameters(request);

            resultObj.setMsg("成功！");
            menuService.deleteMenu(map);
        } catch (MyRunTimeExcption myRunTimeExcption) {

            resultObj.setMsg(myRunTimeExcption.getMessage());
            myRunTimeExcption.printStackTrace();
        } catch (Exception e) {
            resultObj.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return  resultObj.toJson();
    }







}
